package com.danskin.wechat.webapi;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.danskin.wechat.entity.ProductGroupon;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.model.ProductGrouponUniteDTO;
import com.danskin.wechat.model.ProductGrouponUniteDetailedDTO;
import com.danskin.wechat.service.ProductGrouponService;
/***
 * 商品精选(团购)
 * @author admin
 *
 */
@RestController
@RequestMapping("api/productInfo")
public class ProductInfoApiController extends BaseApiController{
	@Resource
	private ProductGrouponService productGrouponService;
	/***
	 * 商品精选列表
	 * @param appId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getProductInfoListApi/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getProductInfoListApi(@PathVariable("appId") String appId,@RequestBody Map<String, Object> map,HttpServletRequest request) {
		if (!map.containsKey("name")) {
			return this.getError("名称能为空");
		}
		String name = map.get("name").toString();
		List<ProductGroupon> productGrouponList = productGrouponService.queryProductGrouponAPIList(appId,name);
		List<ProductGrouponUniteDTO> dtoList = new ArrayList<ProductGrouponUniteDTO>();
		if(productGrouponList.size()>0){
			for(ProductGroupon pg:productGrouponList){
				ProductGrouponUniteDTO dot = new ProductGrouponUniteDTO();
				dot.setId(pg.getId());
				dot.setPrice("0");
				dot.setOriginPoints(pg.getOriginPrice());
				dot.setSalePoints(pg.getSalePrice());
				dot.setTitle(pg.getName());
				dot.setImg(pg.getDetailImg());
				dot.setO2o(pg.getO2o());
				dtoList.add(dot);
			}	
		}
		return new AccessResponse<Object>("0", "ok", dtoList);
	}
	/**
	 * 团购详细信息
	 * @return
	 */
	@RequestMapping(value = "getProductInfoDetailed/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getProductInfoDetailed(@PathVariable("appId") String appId,@RequestBody Map<String, Object> map,HttpServletRequest request){
		if (!map.containsKey("id")) {
			return this.getError("id不能为空");
		}
		String id = map.get("id").toString();
		ProductGroupon groupon = productGrouponService.queryProductGrouponDetailedAPI(appId,id);
		ProductGrouponUniteDetailedDTO dto = new  ProductGrouponUniteDetailedDTO();
		if(groupon!=null){
			dto.setId(groupon.getId());
			dto.setName(groupon.getName());
			dto.setOriginPoints(groupon.getOriginPrice());
			dto.setSalePoints(groupon.getSalePrice());
			dto.setImg(groupon.getIconImg());
			dto.setO2o(groupon.getO2o());
			dto.setDetails(groupon.getDetails());
		}
		return new AccessResponse<Object>("0", "ok", dto);
	}  
}
